#include <iostream>
#include <cstdio>
#include <string>
#include <unistd.h>
#include <pthread.h>
using namespace std;

// 只在Linux有效， __thread只能修饰内置类型（语言自带的最基本的类型）；没有__thread两个共享
__thread int gval = 100;//主线程和新线程各自独有，互不影响   叫做局部存储

std::string ToHex(pthread_t tid)
{
    char id[128];
    snprintf(id, sizeof(id), "0x%lx", tid);
    return id;
}

void* threadrun(void* args)
{
   string name = static_cast<const char *>(args);
    while (true)
    {
        std::cout << name << " is running, tid: "  << ", gval: " << gval << ", &gval: " << &gval << std::endl;
        sleep(1);
    }
}
int main()
{
    pthread_t tid;// tid是线程属性集合的起始虚拟地址--- 在pthread库中维护
    pthread_create(&tid,nullptr,threadrun,(void*)"thread-1");

    while (true)
    {
        std::cout << "main thread, gval: " << gval << ", &gval: " << &gval << std::endl;
        sleep(1);
    }
    pthread_join(tid, nullptr);
    return 0;
}